﻿@using Lavka.Core
@using Lavka.Models
@model PriceProduct

@{
    
    var context = new LavkaDbEntities();
    bool isProductInBase = ProductsHelper.IsProductInBase(Model.Reference, context);
    if(isProductInBase)
    {
        var product = ProductsHelper.GetProductByReference(Model.Reference, context);
        ViewData["Brands"] = new SelectList(context.Brands.ToList(), "BrandId", "Name", product.BrandId);
        ViewData["Categories"] = new SelectList(CategoriesHelper.GetCategories(false, context), "CategoryId", "Name", product.CategoryId);
        Model.Description = product.Description;
        Model.Name = product.Name;
    }
    else
    {
        ViewData["Brands"] = new SelectList(context.Brands.ToList(), "BrandId", "Name");
        ViewData["Categories"] = new SelectList(CategoriesHelper.GetCategories(false, context), "CategoryId", "Name");        
    }
}       
<td style="border: none;">           
@using (Ajax.BeginForm("SendProductToBase", new AjaxOptions
        {
            HttpMethod = "POST",
            UpdateTargetId = Model.Reference,
            InsertionMode = InsertionMode.Replace
        }))
{   
    <table>
        <tr>
            <td>
                @if (!isProductInBase)
                {
                    @Html.Hidden("IsInBase","False")
                    <text>Нет</text>
                }
                else
                {
                    @Html.Hidden("IsInBase", "True")
                    <text><div class="validation">Есть</div></text>
                }
                
            </td>
            <td>
            <td>
                @Html.EditorFor(m => m.Name)
            </td>
            <td>
                <div>Артикул @Html.DisplayFor(m => m.Reference)
                             @Html.HiddenFor(m => m.Reference)
                </div>
                <div>Производитель @Html.DropDownList("ddlBrands", (IEnumerable<SelectListItem>)ViewData["Brands"])</div>
                <div>Категория @Html.DropDownList("ddlCategories", (IEnumerable<SelectListItem>)ViewData["Categories"])</div>
                <div>Цена @Html.EditorFor(m => m.Price)</div>
                <div>Наличие @Html.CheckBoxFor(m => m.Availability, new {@checked = true})</div>
            </td>
            <td>
                @Html.EditorFor(m => m.Description)
            </td>
            <td>
                <div style="color: green"><input id="hUpdateMessage" type="hidden" />@ViewBag.UpdateMessage</div>
                <input id="btnSend" type="submit" value="В базу" />
            </td>  
            <td>
        </tr>
    </table>
}
</td>
